# BUILD
FROM golang:1.22.1-alpine3.19 AS build

# Update packages and clear cache
RUN apk update && apk add --no-cache git curl && rm -rf /var/cache/apk/*
# Set work directory
WORKDIR /app
# Create binary directory
RUN mkdir /app/bin -p
# Create golang migrate util directory
RUN mkdir /bin/lua -p
# Add lua tool
ADD ./tools/ /tools
# Add main files to app
ADD . .
# Download go depences
RUN go mod download
# Build app
RUN go build -o bin ./...

# APP
FROM alpine:3.19 AS deploy

# Install lua dependencies
RUN apk add --no-cache libc6-compat
# Create home directory
WORKDIR /app
# Copy build file
COPY --from=build /app/bin/app ./app
# Copy lua tools files
COPY --from=build /tools/lua/luajit ./lua/luajit
COPY --from=build /tools/lua/jit ./jit

RUN chmod +x ./lua/luajit

# CMD
CMD ["./app"]
EXPOSE 9090